package com.sihai.ehr.web.action.train;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.TrainLecturer;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.TrainLecturerService;
import com.sihai.ehr.web.action.EHRAction;

/**
 * 培训类型控制器
 * 
 * @author henry
 * 
 */
@Results({ @Result(name = "main", location = "train_main.jsp"),
		@Result(name = "lecturer_manage", location = "lecturer_manage.jsp"),
		@Result(name = "list", location = "lecturer_list.jsp") })
public class TrainLecturerAction extends EHRAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Log log = LogFactory.getLog(TrainLecturerAction.class);

	@BeanInject
	private OrganizationService organizationService;
	@BeanInject
	private TrainLecturerService trainLecturerService;

	private List<Organization> organizations;
	private Long dutyId;
	private LimitedList<TrainLecturer> lecturers;
	private TrainLecturer lecturer;
	private Long lecturerId;
	private String ids;

	@Action("/train/lecturer_main")
	@Override
	public String execute() throws Exception {
		listTitle = "内部讲师管理";
		listAction = "/train/lecturer_list.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = true;
		return "main";
	}

	@Action("/train/lecturer_list")
	public String list() {
		listAction = "/train/lecturer_list.xhtml";
		lecturers = trainLecturerService.getTrainLecturers(organizationId,
				dutyId, createLimit(), createSorter());
		paging(lecturers);
		/* 设置翻页action */
		this.setPageUrl("train/lecturer_list.xhtml");
		return "list";
	}

	/**
	 * 分类管理处理
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/train/lecturer_manage")
	public String dispatchLecturer() throws Exception {
		if (lecturerId == null || lecturerId <= 0) {
			// 添加
			lecturer = new TrainLecturer();
		} else {
			// 修改
			lecturer = trainLecturerService.getTrainLecturer(lecturerId);
		}
		return "lecturer_manage";
	}

	@Action("/train/post_lecturer")
	public String postLecturer() throws Exception {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (lecturer == null) {
			if (log.isErrorEnabled()) {
				log.error("提交数据为空");
				return ERROR;
			}
		}
		if (lecturer.getId() == null) {
			// 添加
		} else {
			// 修改
		}
		lecturer.setEffectiveDate(formatter.parse(getParameter("effective_date")));
		lecturer.setExpireDate(formatter.parse(getParameter("expire_date")));

		// 保存数据
		try {
			trainLecturerService.save(lecturer);
		} catch (Exception e) {
			log.equals(e.getMessage());
			return ERROR;
		}
		return "main";
	}

	/**
	 * 删除培训类别
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/train/lecturer_delete", results = { @Result(name = "success", type = "plainText") })
	public String deleteLecturer() throws Exception {
		if (StringUtils.isNotBlank(ids)) {
			try {
				trainLecturerService.deleteTrainLecturer(ids);
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(e.getMessage());
				}
				return ERROR;
			}
		}
		
		return SUCCESS;
	}

	public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}

	public List<Organization> getOrganizations() {
		return organizations;
	}

	public void setDutyId(Long dutyId) {
		this.dutyId = dutyId;
	}

	public Long getDutyId() {
		return dutyId;
	}

	public LimitedList<TrainLecturer> getLecturers() {
		return lecturers;
	}

	public void setLecturers(LimitedList<TrainLecturer> lecturers) {
		this.lecturers = lecturers;
	}

	public TrainLecturer getLecturer() {
		return lecturer;
	}

	public void setLecturer(TrainLecturer lecturer) {
		this.lecturer = lecturer;
	}

	public Long getLecturerId() {
		return lecturerId;
	}

	public void setLecturerId(Long lecturerId) {
		this.lecturerId = lecturerId;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
